﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using IOEx;
using System.Xml;
using System.IO;
using QuanLyTruong.Business;

namespace QuanLyTruong
{
    public partial class frmDangkybanquyen : DevExpress.XtraEditors.XtraForm
    {
        public frmDangkybanquyen()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            frmThongtindangky frmTT = new frmThongtindangky();
            frmTT.parentForm = this;
            frmTT.ShowDialog();
        }

        string CDKey = "";
        public void DoGenerateAfterFillInFormdangky()
        {
            if (txtCDKey1.Text.Length == 4 && txtCDKey2.Text.Length == 4 && txtCDKey3.Text.Length == 4)
            {

                CDKey = txtCDKey1.Text + txtCDKey2.Text + txtCDKey3.Text;

                string newcode = General.GenerateMakhoitao(CDKey);

                if (newcode != "")
                {
                    txtRawkeycode1.Text = newcode.Substring(0, 4);
                    txtRawkeycode2.Text = newcode.Substring(4, 4);
                    txtRawkeycode3.Text = newcode.Substring(8, 4);
                    txtRawkeycode4.Text = newcode.Substring(12, 4);
                }
            }
        }

        private void txtCDKey1_TextChanged(object sender, EventArgs e)
        {
            DoGenerateAfterFillInFormdangky();
        }

        private void txtCDKey2_TextChanged(object sender, EventArgs e)
        {
            DoGenerateAfterFillInFormdangky();
        }

        private void txtCDKey3_TextChanged(object sender, EventArgs e)
        {
            DoGenerateAfterFillInFormdangky();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            //Kiem tra ma kich hoat hop le hay khong?
            string makichhoat = General.GenerateActiveNumber(txtRawkeycode1.Text + txtRawkeycode2.Text + txtRawkeycode3.Text + txtRawkeycode4.Text,CDKey);
            if (makichhoat.ToLower().Trim() == (txtActive1.Text + txtActive2.Text + txtActive3.Text + txtActive4.Text).ToLower().Trim())
            {
                if (File.Exists(Application.StartupPath + "\\config.xml"))
                {
                    XmlDocument xd = new XmlDocument();
                    xd.Load(Application.StartupPath + "\\config.xml");

                    XmlNode xNode = xd.SelectSingleNode("connection/CDKey");
                    xNode.InnerText = CDKey;

                    xNode = xd.SelectSingleNode("connection/ActiveNumber");
                    xNode.InnerText = txtActive1.Text + txtActive2.Text + txtActive3.Text + txtActive4.Text;

                    xd.Save(Application.StartupPath + "\\config.xml");

                }
            }
        }
    }
}